package common

import (
	"github.com/Unknwon/goconfig"
	"github.com/mitchellh/mapstructure"
	"os"
	"runtime"
	"strings"
)

// 网络监听
type network struct {
	Host string
	Port string
	WcPort string
}

// DB连接
type mongoDb struct {
	Host 	string
	Port 	string
	user 	string
	pass 	string
	DbName 	string
}

// 全局配置
type global struct {
	Debug 		string
	Timezone 	string
}

type auth struct {
	JwtSecret 	string
}

var MongoConf 	 	*mongoDb
var NetworkConf  	*network
var GlobalConf   	*global
var AuthConf 	 	*auth

// web 路径
var AppPath        string
var ServerConfPath string 	// 配置文件路径
var ServerLogPath  string	// 日志文件路径





func init()  {
	getWebPathTostring()
	readConfig()
}



// 获取应用路径
func getWebPathTostring()  {
	path ,err := os.Getwd()
	if err != nil {
		panic(err)
	}



	if runtime.GOOS == "windows" {
		s := strings.Split(path,"\\")
		if s[len(s)-1] != "src" {
			path += "/src"
		}
		path += "/ccGame"
	}

	AppPath = path
	ServerConfPath  = path+"/conf/"
	ServerLogPath   = path+"/log/"
}

//
// 获取配置文件数据
//

func readConfig(){
	conf , err := goconfig.LoadConfigFile(ServerConfPath+"config.conf")
	if err != nil {
		panic(err)
	}

	mongo,err := conf.GetSection("mongo")
	if err != nil {
		panic(err)
	}

	err = mapstructure.Decode(mongo,&MongoConf)
	if err != nil {
		panic(err)
	}

	network,err := conf.GetSection("network")
	if err != nil {
		panic(err)
	}
	err = mapstructure.Decode(network,&NetworkConf)
	if err != nil {
		panic(err)
	}
	global,err := conf.GetSection("global")
	if err != nil {
		panic(err)
	}
	err = mapstructure.Decode(global,&GlobalConf)
	if err != nil {
		panic(err)
	}

	auth,err := conf.GetSection("auth")
	if err != nil {
		panic(err)
	}
	err = mapstructure.Decode(auth,&AuthConf)
	if err != nil {
		panic(err)
	}

	return
}




